/*-------------------------------------------------------------------------*/
/**
  @file		execrefine_undo.c
  @author	Renaud Wanchoor
  @date	July 2004
  @version	$Revision: 2.5 $
  @brief	Definition of Undo functions used in the KL stage.

  undo functions were in an early version used more often, i kept the 
  structure of those functions even if it can now be simplified. There are 
  used to decide whether or not a succesion of move must be validated or undone
  Undo function prototype must follow ::UNDO_FN declaration
  
  there are 3 possible action:
  - UNDO_INI (intialization of static variables, before the call to refinekl)
  - UNDO_UPDATE (update static variables, when a vertex is moved)
  - UNDO_TEST (test returing UNDO_APPLY, UNDO_UNDO or UNDO_NOTHING)
   
     - UNDO_APPLY means the succession of move is validated
     - UNDO_UNDO means the succession of moves must be undone
     - UNDO_NOTHING for other cases
  
*/
/*--------------------------------------------------------------------------*/

#include "pagrid.h"
#include "execrefine_params.h"
 
/** 
 * \brief no undo
 *
 * \return UNDO_NOTHING */
int NoUndo(void* ctrl,int action,vRef* v){
  return UNDO_NOTHING;
}

/** 
 * \brief apply moves or a succession of moves that decrease the total execution time
 *
 * It has a static variable that computes the cumumativeDeltaT and apply 
 * succesion of moves or move that have a negative cumulativeDeltaT 
 */
int UndoKl(void* ctrl,int action,vRef* v){
  static double cumul;
  static int    movesize;

  switch(action){
  case UNDO_INI:
    cumul    = 0;
    movesize = 0;
    return UNDO_NOTHING;
    break;
  case UNDO_UPDATE:
    cumul   += v->deltaF + v->deltaT + v->deltaO;
    movesize++;
    return UNDO_NOTHING; 
    break;
  case UNDO_TEST:
    if(cumul < 0)
      return UNDO_APPLY;

    if(movesize >= MAX_KLMOVES)
      return UNDO_UNDO;
        
    return UNDO_NOTHING;
  }

  return UNDO_NOTHING;
}
